package com.thread;

import java.util.concurrent.TimeUnit;

public class ThreadSingleClass009 {
    public static ThreadSingleClass009 INSTANCE;

    //synchronized 这里锁粒度太重，有没有更好优化方法
    public static /*synchronized*/ ThreadSingleClass009 getInstance() {

        if (INSTANCE == null) {
            //试图进行锁粒度优化，然而不行，存在多并发问题
            synchronized (ThreadSingleClass009.class) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                INSTANCE = new ThreadSingleClass009();
            }
        }

        return INSTANCE;
    }

    private ThreadSingleClass009() {
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; ++i) {
            new Thread(() -> {
                System.out.println(ThreadSingleClass009.getInstance().hashCode());
                ;
            }).start();
        }
    }
}
